<?php

/*
 +------------------------------------------------------------------------+
 | Phosphorum                                                             |
 +------------------------------------------------------------------------+
 | Copyright (c) 2013-2014 Phalcon Team and contributors                  |
 +------------------------------------------------------------------------+
 | This source file is subject to the New BSD License that is bundled     |
 | with this package in the file docs/LICENSE.txt.                        |
 |                                                                        |
 | If you did not receive a copy of the license and are unable to         |
 | obtain it through the world-wide-web, please send an email             |
 | to license@phalconphp.com so we can send you a copy immediately.       |
 +------------------------------------------------------------------------+
*/


/**
 * This scripts generates random posts
 */

require 'cli-bootstrap.php';

$faker = Faker\Factory::create();
$log   = new Phalcon\Logger\Adapter\Stream('php://stdout');
$log->info('Start');
/** @var Phalcon\Db\AdapterInterface $database */
$database = $di->getShared('db');

$database->begin();

/*
for ($i = 0; $i <= 5; $i++) {

    $title = $faker->company;
    $file = new Faker\Provider\File($faker);
    //aer$filePath = \Faker\Provider\File::file('/tmp' , '/public/files/video' , true);
    die(print $filePath);

    $videoFile = new \Common\Models\Videos\VideoFile();
    $videoFile->setName($faker->name);
    $videoFile->setSourceServer('J');
    $videoFile->setQuanlity(720);
    $videoFile->setLinkSourceServer($file::mimeType());
    $videoFile->setFilePath($file::mimeType());
    $videoFile->setAgent('F');
    $videoFile->setStatus('A');
    try{
        if (!$videoFile->save()) {
            var_dump($this->flash->error($videoFile->getMessages()));
            $database->rollback();
        }
    }
    catch (Phalcon\Exception $e)
    {
        echo $e->getMessage();
    }
}
$database->commit();
*/
/*

for ($i = 0; $i <= 200; $i++) {

    $title = $faker->company;
    $file = new Faker\Provider\File($faker);
    //aer$filePath = \Faker\Provider\File::file('/tmp' , '/public/files/video' , true);
    //die(print $filePath);

    $artist = new \Common\Models\Videos\Artist();
    $artist->setName($faker->name);
    $artist->setArtistType(2);
    $artist->setGender(1);
    $artist->setNationalId(1);

    try{
        if (!$artist->save()) {
            var_dump($this->flash->error($video->getMessages()));
            $database->rollback();
        }
    }
    catch (Phalcon\Exception $e)
    {
        echo $e->getMessage();
    }
}
$database->commit();*/

/*
for ($i = 0; $i <= 10; $i++) {

    $title = $faker->company;
    $file = new Faker\Provider\File($faker);
    //aer$filePath = \Faker\Provider\File::file('/tmp' , '/public/files/video' , true);
    //die(print $filePath);

    $data = new \Common\Models\Videos\HomeCategory();
    $data->setName($faker->name);
    $data->setStatus('A');

    try{
        if (!$data->save()) {
            var_dump($this->flash->error($data->getMessages()));
            $database->rollback();
        }
    }
    catch (Phalcon\Exception $e)
    {
        echo $e->getMessage();
    }
}
$database->commit();*/
/*
$homeCategories = \Common\Models\Videos\HomeCategory::find();
foreach($homeCategories as $homeCategory)
{
    $videos = \Common\Models\Videos\Video::find(array(
        "limit" => 10,
        "order" => "RAND()"
    ));

    foreach($videos as $video)
    {
        $data = new \Common\Models\Videos\HomeVideo();
        $data->setObjectId($video->getId());
        $data->setObjectType('video');
        $data->setHomeCategoryId($homeCategory->getId());
        $data->setStatus('A');
        try{
            if (!$data->save()) {
                var_dump($this->flash->error($data->getMessages()));
                $database->rollback();
            }
        }
        catch (Phalcon\Exception $e)
        {
            echo $e->getMessage();
        }
    }
}
$database->commit();
*/

for ($i = 0; $i <= 10; $i++) {
    $title = $faker->company;
    $file = new Faker\Provider\Image($faker);
    //aer$filePath = \Faker\Provider\File::file('/tmp' , '/public/files/video' , true);
    try{
        $imgUrl = $file->image('../files' , 1600 , 600 , null , false);
        $upload = new \Library\Upload\UploadFile();
        //$image = new Phalcon\Image\Adapter\GD($imgUrl);
        //$image->save();


        $listThumbnail = \Common\Library\Common::listThumbnailOptionA();

        if($listThumbnail != null)
        {
            foreach($listThumbnail as $key => $thumbnail)
            {
                $dirPathThumbnail = '../files' . '/thumbnail/' . $key . '/';
                if (!file_exists($dirPathThumbnail) && !is_dir($dirPathThumbnail)) {
                    mkdir($dirPathThumbnail , 0777);
                }

                $imagePathThumbnail = $dirPathThumbnail . $imgUrl;
                //die(print $imagePathThumbnail);

                $imageUpload = new \Phalcon\Image\Adapter\GD('../files/' . $imgUrl);
                $imageUpload->resize($thumbnail['width'] , $thumbnail['height']);
                $upload = $imageUpload->save($imagePathThumbnail , 100);
            }
        }

    }
    catch (Phalcon\Exception $e)
    {
        echo $e->getMessage();
    }

    $data = new \Common\Models\Images\Images();
    $data->setImagePath($imgUrl);

    try{
        if (!$data->save()) {
            var_dump($this->flash->error($data->getMessages()));
            $database->rollback();
        }
    }
    catch (Phalcon\Exception $e)
    {
        echo $e->getMessage();
    }
}
$database->commit();